#include <QCoreApplication>
#include <Qstring>
#include <iostream>
#include <memory>
class myclass {

public:
  int a;
  QString number;

public:
  myclass(int a, QString number);
};

void testDeleteSerial() {}

std::shared_ptr<myclass> getPointer() {

  // 错误示例，将对象引用传递给只能指针
  // 智能指针在作用域结束后会delete
  // 构造的指针，如果传递的是对象的引用而不是指针，delete函数会报错
  myclass my2(2, "getNumber111");
  std::shared_ptr<myclass> share_class5(&my2);

  // 正常用法1
  myclass *my1 = new myclass(2, "getNumber111");
  std::shared_ptr<myclass> share_class2(my1);

  // 正常用法2
  return std::make_shared<myclass>(2, "newGetnum");
}
int main(int argc, char *argv[]) {
  QCoreApplication a(argc, argv);

  myclass my1(1, "classnumber");
  std::shared_ptr<myclass> share_class = std::make_shared<myclass>(1, "123");
  std::shared_ptr<myclass> share_class2(&my1);
  std::cout << share_class.get()->a << std::endl;
  std::cout << share_class.get() << std::endl;
  std::cout << share_class << std::endl;
  std::cout << "share_class2 usecount:" << share_class2.use_count()
            << std::endl;
  std::cout << "share_class2:" << share_class2->number.toStdString()
            << std::endl;
  std::shared_ptr<myclass> share_class3 = getPointer();

  std::cout << share_class3->a << std::endl;
  std::cout << share_class3->number.toStdString() << std::endl;
  return a.exec();
}

myclass::myclass(int a, QString number) : a(a), number(std::move(number)) {}
